﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagerSystem
{
    //单例模式
    public class StudentsSystem : Singleton<StudentsSystem>
    {
        public E_PermissionType Permission;
        public InputManager InputManager;
        private List<Student> StudentsList;
        Random random = new Random();

        public StudentsSystem()
        {
            InputManager = new InputManager(this);
            StudentsList = new List<Student>();
            TestData();
        }

        #region Test Data
        private void TestData()
        {
            for (E_GradeType i = E_GradeType.None; i < E_GradeType.Four; i++)
            {
                for (int j = 0; j < 1000; j++)
                {
                    TestAddStudent(i + 1, j);
                }
            }
        }

        private void TestAddStudent(E_GradeType grade, int id)
        {
            Student s1 = new Student();
            s1.ID = ((int)grade)*100000+id;
            s1.Name = "name "+id;
            s1.Sex = (E_SexType)(random.Next()%2+1);
            s1.Grade = grade;
            s1.Class = random.Next()% 24+1;
            for (E_CourseType i = E_CourseType.None; i < E_CourseType.Chemistry; i++)
            {
                s1.Score.SetScore(i + 1, 75 + random.Next()%50-25);
            }
            AddStudent(s1);
        }

        #endregion

        /// <summary>
        /// 系统入口
        /// </summary>
        public void Start()
        {
            //画一个登录界面
            InputManager.LoginPanel();
            //InputManager.MainPanel();
        }

        /// <summary>
        /// 验证账号密码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckAccountPassword(string account, string password)
        {
             /*
        None,
        Root,              //超级管理员
        Administrator,     //普通管理员
        Teacher,           //老师
        Student,           //学生
        Visitor,           //游客
        */
            if ((account == "Root" || account == "Administrator" || account == "Teacher") && password == "123456")
            {
                Permission = E_PermissionType.Root;
                return true;
            } else if ((account == "Student" || account == "Visitor" || account == "None") && password == "123456")
            {
                Permission = E_PermissionType.Student;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 添加学生
        /// </summary>
        /// <param name="s"></param>
        public void AddStudent(Student s)
        {
            if (IsHasStudent(s))
            {
                Student student = GetStudent(s.ID);
                student = s;
            }
            else
            {
                StudentsList.Add(s);
            }
        }

        /// <summary>
        /// 根据学号获取学生
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Student GetStudent(int id)
        {
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].ID == id)
                    return StudentsList[i];
            }
            return null;
        }


        public Student GetStudent(string name)
        {
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (name == StudentsList[i].Name)
                    return StudentsList[i];
            }
            return null;
        }

        /// <summary>
        /// 根据学号移除学生
        /// </summary>
        /// <param name="id"></param>
        public void RemoveStudent(int id)
        {
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].ID == id)
                {
                    StudentsList.RemoveAt(i);
                    break;
                }
            }
        }
        /// <summary>
        /// 获取指定年级的学生
        /// </summary>
        /// <param name="grade"></param>
        /// <returns></returns>
        public List<Student> GetStudents(E_GradeType grade)
        {
            List<Student> ret = new List<Student>();
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].Grade == grade)
                    ret.Add(StudentsList[i]);
            }
            return ret;
        }

        public List<Student> GetStudents(E_GradeType grade, int classNum = 0, E_CourseType course = E_CourseType.None)
        {
            List<Student> ret = new List<Student>();
            for (int i = 0; i < StudentsList.Count; i++)
            {
                Student s = StudentsList[i];
                //获取指定年级学生
                if (classNum == 0)
                {
                    if (s.Grade == grade)
                        ret.Add(s);
                }
                else//获取指定班级学生
                {
                    if (s.Grade == grade && s.Class == classNum)
                        ret.Add(s);
                }
            }



            if (course == E_CourseType.None)
                ret.Sort(new ScoreCompare(E_CourseType.All));
            else
                ret.Sort(new ScoreCompare(course));
            return ret;
        }



        /// <summary>
        /// 获取指定年级所有成绩
        /// </summary>
        /// <param name="grade"></param>
        /// <returns></returns>
        public List<Score> GetScores(E_GradeType grade)
        {
            List<Score> ret = new List<Score>();
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].Grade == grade)
                    ret.Add(StudentsList[i].Score);
            }
            return ret;
        }
        /// <summary>
        /// 获取指定年级指定科目成绩
        /// </summary>
        /// <param name="grade"></param>
        /// <param name="course"></param>
        /// <returns></returns>
        public List<float> GetScores(E_GradeType grade, E_CourseType course)
        {
            List<float> ret = new List<float>();
            for (int i = 0; i < StudentsList.Count; i++)
            {
                ret.Add(StudentsList[i].Score.GetScore(course));
            }
            return ret;
        }
        /// <summary>
        /// 判断学生是否存在
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public bool IsHasStudent(Student s)
        {
            return IsHasStudent(s.ID);
        }
        /// <summary>
        /// 判断学生是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsHasStudent(int id)
        {
            for (int i = 0; i < StudentsList.Count; i++)
            {
                if (StudentsList[i].ID == id)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
